package com.ss.android.b;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;

public final class d extends AbsApiThread {
    private final AdManager b;
    private final int a;

    public d(AdManager arg1, String arg2, Priority arg3, int arg4) {
        super(arg2, arg3);
        this.b = arg1;
        this.a = arg4;

    }

    public final void run() {
        int v0_6;
        SharedPreferences.Editor v8_2;
        Object v5_3;
        String v1_1;
        String v2_3;
        String v3_2;
        AdManager.c$c v11;
        JSONArray v10;
        JSONObject v9;
        JSONArray v8_1;
        JSONArray v5_2;
        long v6_1;
        String v0_3;
        StringBuilder v3;
        long v12 = 0;
        AdManager v4 = this.b;
        int v2 = this.a;
        int v1 = 18;
        try {
            v3 = new StringBuilder("http://ib.snssdk.com/service/14/app_ad/");
            v3.append("?_unused=0");
            Object v0_2 = v4.mContext.getSystemService(Context.TELEPHONY_SERVICE);
            String v5 = ((TelephonyManager)v0_2).getNetworkOperatorName();
            if(!android.support.design.a.isTextEmpty(v5)) {
                v3.append("&carrier=").append(Uri.encode(v5));
            }

            v0_3 = ((TelephonyManager)v0_2).getNetworkOperator();
            if(android.support.design.a.isTextEmpty(v0_3)) {
            }else {
                v3.append("&mcc_mnc=").append(Uri.encode(v0_3));
            }
            DisplayMetrics v0_4 = v4.mContext.getResources().getDisplayMetrics();
            v3.append("&ad_area=").append(v0_4.widthPixels);
            v3.append("x").append(v0_4.heightPixels - UIUtils.getStatusBarHeight(v4.mContext));
            if(v2 > 0) {
                v3.append("&bh=").append(v2);
            }
try{
            v3.append("&display_density=").append(v0_4.widthPixels);
            v3.append("x").append(v0_4.heightPixels);
            v3.append("&display_dpi=").append(v0_4.densityDpi);
            v3.append("&density=").append(v0_4.density);
        } catch(Exception v0_1) {
                Logger.w("AdManager", "can not get display metrics: " + v0_1);
        }

            v0_3 = NetworkUtils.getNetworkAccessType(v4.mContext);
            if(!android.support.design.a.isTextEmpty(v0_3)) {
                v3.append("&access=").append(v0_3);
            }

            v0_3 = v3.toString();
            ArrayList v2_1 = new ArrayList();
            SharedPreferences v3_1 = v4.mContext.getSharedPreferences("ss_splash_ad", 0);
            SharedPreferences.Editor v5_1 = v3_1.edit();
            String v6 = v3_1.getString("last_logs", "");
            String v7 = v3_1.getString("preload_ids", "");
            long v8 = v3_1.getLong("fetch_time", 0);
            if(!android.support.design.a.isTextEmpty(v6)) {
                v2_1.add(new NameValueImpl("last_log_list", v6));
            }

            if(!android.support.design.a.isTextEmpty(v7)) {
                v2_1.add(new NameValueImpl("preload_list", v7));
            }

            if(v8 > v12) {
                v2_1.add(new NameValueImpl("fetch_time", String.valueOf(v8)));
            }

            v5_1.remove("last_logs");
            v5_1.remove("preload_ids");
            SharedPreferenceCompat.commit(v5_1);
            if(v4.h != null) {
                v4.h.clear();
            }

            if(v4.i != null) {
                v4.i.clear();
            }

            v0_3 = NetworkUtils.executePost(-1, v0_3, ((List)v2_1));
            if(!android.support.design.a.isTextEmpty(v0_3)) {
                v6_1 = System.currentTimeMillis();
                JSONObject v2_2 = new JSONObject(v0_3);
                if(AbsApiThread.isApiSuccess(v2_2)) {
                    JSONObject v0_5 = v2_2.getJSONObject("data");
                    v5_2 = v0_5.optJSONArray("splash");
                    v8_1 = v0_5.optJSONArray("ad_area");
                    v9 = v0_5.optJSONObject("app_bind");
                    v10 = v0_5.optJSONArray("launcher_ads");
                    v11 = new AdManager.c$c((byte) 0);
                    v12 = v0_5.optLong("show_adsage_close_button_delay");
                    v11.a(v5_2, v6_1);
                    v11.a(v8_1);
                    v11.a(v9);
                    v3_2 = "";
                    v2_3 = "";
                    v1_1 = "";
                    v0_3 = "";
                    if(v5_2 != null) {
                        v3_2 = v5_2.toString();
                    }

                    if(v8_1 != null) {
                        v2_3 = v8_1.toString();
                    }

                    if(v9 != null) {
                        v1_1 = v9.toString();
                    }

                    if(v10 != null) {
                        v0_3 = v10.toString();
                    }

                    v5_3 = AdManager.a;
                    synchronized (v5_3) {
                        v8_2 = v4.mContext.getSharedPreferences("ss_splash_ad", 0).edit();
                        if (!TextUtils.isEmpty(v3_2)) {
                            v8_2.putString("splash_str", v3_2);
                        }

                        if (!TextUtils.isEmpty(((CharSequence) v2_3))) {
                            v8_2.putString("ad_area_str", v2_3);
                        }

                        if (!TextUtils.isEmpty(((CharSequence) v1_1))) {
                            v8_2.putString("bind_app_str", v1_1);
                        }

                        if (!TextUtils.isEmpty(((CharSequence) v0_3))) {
                            v8_2.putString("launcher_ads_str", v0_3);
                        } else {
                            v8_2.putString("launcher_ads_str", "");
                        }
                        v8_2.putLong("fetch_time", v6_1);
                        v8_2.putLong("adsage_close_protect", v12);
                        SharedPreferenceCompat.commit(v8_2);
                    }
                    Message v0_7 = v4.b.obtainMessage(10);
                    v0_7.obj = v11;
                    v4.b.sendMessage(v0_7);
                    v4.b.sendEmptyMessage(103);
                    v4.b.sendEmptyMessage(104);
                    return;
                }
            }
            v0_6 = v1;
        } catch(Throwable v0) {
            Throwable v1_2 = v0;
            v0_6 = com.ss.android.b.bb.a().a(v4.mContext, v1_2);
            Logger.d("AdManager", "get ad error:" + v1_2);
        }

        Message v1_3 = v4.b.obtainMessage(11);
        v1_3.arg1 = v0_6;
        v4.b.sendMessage(v1_3);
        return;




    }
}

